﻿using RLRT.Common;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using RLRT.Models;

namespace RLRT.Web
{
    public partial class ReceivingCancel : LocalPage
    {
        /// <summary>
        /// 页面加载
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                #region 验证权限

                #endregion

                #region 获取参数

                #endregion

                //初始化
                this.Initialize();
            }

            #region 页面标题

            this.Title = PageTitle.Text;

            #endregion

            #region 页面要素


            #endregion
        }

        #region 初始化

        /// <summary>
        /// 初始化
        /// </summary>
        private void Initialize()
        {
            #region 页面内容
            using (RTProvider rtProvider = new RTProvider())
            {
                var rtnos = rtProvider.GetCancelRTNoList();
                LocalGlobal.BindListItems(RTNo, rtnos.DefaultView, "rtrno", "rtrno", true);
            }
            #endregion

            //绑定列表
            //this.BindList();
        }

        #endregion

        #region 绑定数据

        protected void RTNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindList();
        }

        /// <summary>
        /// 绑定列表
        /// </summary>
        private void BindList()
        {
            using (RTProvider rtProvider = new RTProvider())
            {
                string rtNo = this.RTNo.SelectedValue.Trim();
                if (rtNo.Length > 0)
                {
                    List.DataSource = rtProvider.GetRTCancelList(rtNo);
                }
            }
            List.DataBind();
        }

        /// <summary>
        /// 行绑定
        /// </summary>
        protected void List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                //数据行
                case DataControlRowType.DataRow:
                    #region 数据绑定

                    ////数据
                    //string rowStatus = ((DataRowView)e.Row.DataItem)["Status"].ToString().Trim();

                    ////控件
                    //HtmlInputCheckBox rowCheckControl = (HtmlInputCheckBox)e.Row.FindControl("RowCheck");

                    ////指派
                    //rowCheckControl.Disabled = (rowStatus == "S");

                    #endregion
                    break;

                case DataControlRowType.EmptyDataRow:
                case DataControlRowType.Header:
                case DataControlRowType.Separator:
                case DataControlRowType.Pager:
                case DataControlRowType.Footer:
                default:
                    break;
            }
        }

        #endregion

        #region 操作

        /// <summary>
        /// 确定
        /// </summary>
        protected void OKButton_Click(object sender, EventArgs e)
        {
            SqlConnection con = LocalGlobal.DbConnect();
            con.Open();
            SqlTransaction tran = con.BeginTransaction();//使用事务
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.Transaction = tran;

            try
            {
                using (RTProvider rtProvider = new RTProvider())
                {
                    bool flag = false;
                    string rtNo = this.RTNo.SelectedValue.Trim();
                    foreach (GridViewRow row in List.Rows)
                    {
                        HtmlInputCheckBox rowCheckControl = (HtmlInputCheckBox)row.FindControl("RowCheck");
                        if (rowCheckControl.Checked)
                        {
                            string seq = row.Cells[1].Text.Trim();
                            cmd.CommandText = rtProvider.CancelRTRecordSql(rtNo, seq);
                            cmd.ExecuteNonQuery();

                            cmd.CommandText = rtProvider.DecreasePRQtySql(row.Cells[3].Text.Trim(), row.Cells[4].Text.Trim(), row.Cells[10].Text.Trim());
                            cmd.ExecuteNonQuery();

                            cmd.CommandText = rtProvider.DecreaseInventorySql(row.Cells[11].Text.Trim(), row.Cells[6].Text.Trim(), row.Cells[10].Text.Trim());
                            cmd.ExecuteNonQuery();

                            flag = true;
                        }
                    }

                    if (!flag)
                    {
                        this.ShowInfoMessage(this.GetGlobalResourceString("NotSelectMessage"));
                        tran.Rollback();
                        return;
                    }
                }
            }
            catch (Exception error)
            {
                tran.Rollback();
                this.ShowErrorMessage("操作失败。" + error.Message);
                return;
            }

            tran.Commit();

            List.DataSource = null;
            List.DataBind();
            Initialize();
        }

        #endregion

    }
}